package com.woniu.controller;

import com.woniu.entity.idiom.IdiomExamEntity;
import com.woniu.entity.idiom.IdiomInfoEntity;
import com.woniu.mapper.idiom.IdiomInfoMapper;
import com.woniu.service.idiom.IdiomExamService;
import com.woniu.service.idiom.IdiomInfoService;
import com.woniu.service.idiom.IdiomInviteRecService;
import com.woniu.service.idiom.IdiomRedBeansRecService;
import com.woniu.utils.ParseExcel;
import com.woniu.vo.Excel;
import com.woniu.vo.idiom.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author liangpei
 * @Date 2024/3/31
 * @Description 成语
 **/
@RestController
@RequestMapping("/idiom")
@Api(tags = "成语")
public class IdiomController {
    private final static Logger logger = LoggerFactory.getLogger(IdiomController.class);

    @Autowired
    private IdiomInfoService idiomInfoService;

    @Autowired
    private IdiomInfoMapper mapper;

    @Autowired
    private IdiomExamService idiomExamService;

    @Autowired
    private IdiomRedBeansRecService idiomRedBeansRecService;



    @ApiOperation(value = "查看成语主信息")
    @PostMapping("/info")
    public IdiomInfoRespDto getIdiomInfo() {
        return idiomInfoService.queryIdiomInfo();
    }

    @ApiOperation(value = "成语答题详情页")
    @GetMapping("/examDetail")
    public List<IdiomExamDetailResp> examDetail(@RequestParam(value = "idiomId") int idiomId) {
        return idiomExamService.getDetailByIdiomId(idiomId);
    }

    @ApiOperation(value = "成语答题")
    @PostMapping("/answer")
    public IdiomExamAnswerResp answer(@RequestBody IdiomExamAnswerReq idiomExamReq) {
        return idiomExamService.answer(idiomExamReq);
    }

    @ApiOperation(value = "阅读完文字加红豆")
    @PostMapping("/read")
    public IdiomExamAnswerResp read() {
        return idiomExamService.read();
    }

    @ApiOperation(value = "我的页面")
    @PostMapping("/myDetail")
    public MyInfoResp myDetail() {
        return idiomInfoService.my();
    }

    //提现 wechatPayAsync
    @ApiOperation(value = "提现")
    @PostMapping("/withdraw")
    public WithDrawResp withdraw () {
        return idiomInfoService.withdraw();
    }

    @ApiOperation(value = "红豆获取记录")
    @PostMapping("/redBeansRec")
    public List<RedBeansRecResp> redBeansRec() {
        return idiomRedBeansRecService.redBeansRec();
    }

    @ApiOperation(value = "提现记录")
    @PostMapping("/withdrawRec")
    public List<RedBeansRecResp> withdrawRec() {
        return idiomRedBeansRecService.withdrawRec();
    }

    @PostMapping("/excel")
    public void excel (@RequestParam("file") MultipartFile file) {
        try {
            List<Excel> excelList = ParseExcel.parseExcel(file.getInputStream(), Excel.class);
            for(Excel e: excelList){
                IdiomInfoEntity idiomInfoEntity = new IdiomInfoEntity();
                idiomInfoEntity.setIdiomName(e.get成语名称());
                idiomInfoEntity.setIdiomApply(e.get成语运用());
                idiomInfoEntity.setIdiomFrom(e.get成语出处());
                idiomInfoEntity.setIdiomHistory(e.get成语故事());
                idiomInfoEntity.setIdiomMoral(e.get成语寓意());
                mapper.insert(idiomInfoEntity);
                int id = idiomInfoEntity.getId();

                IdiomExamEntity examEntityA = new IdiomExamEntity();
                examEntityA.setIdiomId(id);
                examEntityA.setOptionA(e.get问题1选项A());
                examEntityA.setOptionB(e.get问题1选项B());
                examEntityA.setOptionC(e.get问题1选项C());
                examEntityA.setOptionD(e.get问题1选D());
                examEntityA.setAnswer(e.get问题1答案());
                examEntityA.setTitle(e.get问题1标题());
                idiomExamService.save(examEntityA);

                IdiomExamEntity examEntityB = new IdiomExamEntity();
                examEntityB.setIdiomId(id);
                examEntityB.setOptionA(e.get问题2选项A());
                examEntityB.setOptionB(e.get问题2选项B());
                examEntityB.setOptionC(e.get问题2选项C());
                examEntityB.setOptionD(e.get问题2选D());
                examEntityB.setAnswer(e.get问题2答案());
                examEntityB.setTitle(e.get问题2标题());
                idiomExamService.save(examEntityB);


                IdiomExamEntity examEntityC = new IdiomExamEntity();
                examEntityC.setIdiomId(id);
                examEntityC.setOptionA(e.get问题3选项A());
                examEntityC.setOptionB(e.get问题3选项B());
                examEntityC.setOptionC(e.get问题3选项C());
                examEntityC.setOptionD(e.get问题3选D());
                examEntityC.setAnswer(e.get问题3答案());
                examEntityC.setTitle(e.get问题3标题());
                idiomExamService.save(examEntityC);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
